'''
version: V0.1
Author: xubing
Date: 2020-12-11 15:12:52
LastEditors: xubing
LastEditTime: 2020-12-11 18:21:35
Description: 
'''
from mdutils.fileutils.fileutils import MarkDownFile
from conf import config
import pandas as pd
from mdutils import MdUtils, TextUtils
from mdutils.tools.Header import Header
from mdutils.tools.Image import Image
from src.utils.logger import get_logger
import uuid
# logger = get_logger('报告写入')


class MdReport:
    def __init__(self, file_name, title='Title') -> None:
        super().__init__()
        self.md = MdUtils(file_name=file_name, title=title)
        self.logger = get_logger('报告写入')
        self.init()

    def init(self):
        '''
        其他的一些初始化工作
        '''
        self.md.new_paragraph(f'> 报告生成时间:%s' % config.current_time)
        # self.md.new_paragraph(f'> 训练数据日期:%s' % config.train_data_date)
        # self.md.new_paragraph(f'> 预测数据日期:%s' % '2018-12-31')

        self.md.new_paragraph()

    def write_title(self, hn, hn_title):
        self.md.new_header(level=hn, title=hn_title, style='setext')

    def write_table_of_contents(self):
        self.md.new_table_of_contents(table_title='Contents', depth=2)

    def write_paragraph(self, new_paragraph):
        self.md.new_paragraph(new_paragraph)

    def write_table(
        self,
        df,
        limit=100,
        display_all=False,
    ):
        """
        在报告中添加表格数据
        :param df: 待添加的表格数据
        :param limit: 限制显示的行数
        :param display_all: 是否即使超过显示显示的行数依然强制显示全部

        :return:
        """

        if len(df) == 0:
            self.logger.warn(f'dataframe空')
            self.logger.warn(df.columns.tolist())
            self.md.new_paragraph(
                f"数据量: {len(df):,}，列数: {df.shape[1]}，表格列名：{', '.join(['`' + item + '`' for item in df.columns])}"
            )
            return
        else:
            self.md.new_paragraph(f"> 数据量: {len(df):,}，列数: {df.shape[1]}")

        if len(df) < limit or display_all:
            df_sample = df
        else:
            df_sample = df[:limit]

        content_list = df_sample.columns.tolist()
        # content_list.extend([
        #     f'{cell:6f}' if type(cell).__name__ == 'float' else str(cell) for row in df_sample.values
        #     for cell in row
        # ])
        content_list.extend([str(cell)
                             for row in df_sample.values for cell in row])
        self.md.new_table(
            columns=df_sample.shape[1], rows=df_sample.shape[0] + 1, text=content_list)

    def create_report(self):
        self.md.create_md_file()


if __name__ == '__main__':
    mdFile = MdUtils(file_name='Example_Markdown',
                     title='Markdown File Example')
    list_of_strings = ["Items", "Descriptions", "Data"]
    for x in range(5):
        list_of_strings.extend(
            ["Item " + str(x), "Description Item " + str(x), str(x)])
    print(list_of_strings)
    mdFile.new_line()
    mdFile.new_table(columns=3, rows=6, text=list_of_strings,
                     text_align='center')
    mdFile.create_md_file()
